<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册页面</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css">
    <style>
        body, html {
          height: 100%;
          margin: 0;
          background-color: #e9f5f9;
        }
        .container {
          height: 100%;
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 20px;
        }
        .login-register {
          width: 100%;
          max-width: 450px;
          max-height: 750px;
          padding: 35px;
          background-color: #ffffff;
          border-radius: 10px;
          box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
          min-height: 480px;
          display: flex;
          flex-direction: column;
          justify-content: center;
        }
        .logo {
          text-align: center;
          margin-bottom: 5px;
        }
        .logo img {
          width: 80px;
          height: auto;
        }
        h2 {
          text-align: center;
          margin-bottom: 20px;
          color: #333;
        }
        .btn-success {
          border: none;
          font-size: 16px;
          padding: 10px 0;
          border-radius: 5px;
          transition: all 0.3s ease;
        }
        .btn-success:hover {
          background-color: #218838;
        }
        .form-group {
          margin-bottom: 20px;
        }
        .form-control {
          border-radius: 5px;
          height: 45px;
        }
        .form-control:focus {
          border-color: #007bff;
          box-shadow: 0 0 5px rgba(0, 123, 255, 0.25);
        }
    </style>
</head>
<body>
<div class="container">
    <div class="login-register">
        <h2>论文期刊管理系统</h2>
        <div id="errorMsg" style="color:red;"></div>
        <div class="form-group">
            <label for="registerUsername">用户名</label>
            <input type="text" class="form-control" id="registerUsername" placeholder="请输入有效的用户名" required>
        </div>
        <div class="form-group">
            <label for="registerEmail">邮箱</label>
            <input type="email" class="form-control" id="registerEmail" placeholder="请输入您的邮箱地址" required>
        </div>
        <div class="form-group">
            <label for="registerPassword">密码</label>
            <input type="password" class="form-control" id="registerPassword" placeholder="请输入您的密码" required>
        </div>
        <div class="form-group">
            <label for="registerRole">身份</label>
            <select class="form-control" id="registerRole" required>
                <option value="USER">普通用户</option>
                <option value="ADMIN">管理员</option>
            </select>
        </div>
        <button id="registerBtn" class="btn btn-success btn-block">注册</button>
        <div class="text-center">
            <a href="/login">已有账户？点击登录</a>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.slim.min.js"></script>
<script>
    document.getElementById('registerBtn').addEventListener('click', function() {
        const username = document.getElementById('registerUsername').value;
        const email = document.getElementById('registerEmail').value;
        const password = document.getElementById('registerPassword').value;
        const role = document.getElementById('registerRole').value;

        const data = {
            username: username,
            email: email,
            password: password,
            role: role
        };

        fetch('/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        })
        .then(response => {
            if (response.ok) {
                window.location.href = '/login';  // 注册成功后跳转到登录页面
            } else {
                return response.json().then(err => { throw err; });
            }
        })
        .catch(error => {
            document.getElementById('errorMsg').innerText = error.message || '注册失败，请重试。';
        });
    });
</script>
</body>
</html>
